#include<stdio.h>
#include<thread>
#include<vector>
#include"ConcurrentAlloc.h"

void thread1(){
    void*p;
    for(int i=0;i<1024;i++){
        p=ConcurrentAlloc(7);
    }
    p=ConcurrentAlloc(7);
}

void thread2(){
    for(int i=0;i<5;i++)
        ConcurrentAlloc(1027);
}

void TestConcurrentAlloc()
{
    void* p1 = ConcurrentAlloc(6);
    void* p2 = ConcurrentAlloc(8);
    void* p3 = ConcurrentAlloc(1);
    void* p4 = ConcurrentAlloc(7);
    void* p5 = ConcurrentAlloc(8);
    void* p6 = ConcurrentAlloc(6);
    void* p7 = ConcurrentAlloc(7);
    void* p8 = ConcurrentAlloc(7);

    std::cout << p1 << std::endl;
    std::cout << p2 << std::endl;
    std::cout << p3 << std::endl;
    std::cout << p4 << std::endl;
    std::cout << p5 << std::endl;
    std::cout << p6 << std::endl;
    std::cout << p7 << std::endl;
    std::cout << p8 << std::endl;

    ConcurrentFree(p1);
    ConcurrentFree(p2);
    ConcurrentFree(p3);
    ConcurrentFree(p4);
    ConcurrentFree(p5);
    ConcurrentFree(p6);
    ConcurrentFree(p7);
    ConcurrentFree(p8);
}

void MultiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 10000; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
        //std::cout<<"i:"<<ptr<<std::endl;
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		//ConcurrentFree(e, 6);
        ConcurrentFree(e);
	}
}

void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 10000; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		//ConcurrentFree(e, 6);
		ConcurrentFree(e);
	}
}

void TestMultiThread()
{
	std::thread t1(MultiThreadAlloc1);
	std::thread t2(MultiThreadAlloc2);

	t1.join();
	t2.join();
}

void BigAlloc()
{
	void* p1 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
	ConcurrentFree(p2);
}

int main(){
    //std::thread t1(thread1);
    //std::thread t1(TestConcurrentAlloc);
    // std::thread t2(thread2);
    //t1.join();
    //t2.join();
    TestConcurrentAlloc();
    //TestMultiThread();
    //BigAlloc();
    return 0;
}